package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ioftv.entity.ExcellentLabeler;
import com.ioftv.mapper.ExcellentLabelerMapper;
import com.ioftv.service.ExcellentLabelerService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优秀标注员Service实现类
 */
@Service
public class ExcellentLabelerServiceImpl extends BaseService<ExcellentLabelerMapper, ExcellentLabeler> implements ExcellentLabelerService {

    @Override
    public List<ExcellentLabeler> getRanking(Integer year, Integer month, Integer limit) {
        LambdaQueryWrapper<ExcellentLabeler> queryWrapper = createUserQueryWrapper();
        if (year != null) {
            queryWrapper.eq(ExcellentLabeler::getStatYear, year);
        }
        if (month != null) {
            queryWrapper.eq(ExcellentLabeler::getStatMonth, month);
        }
        queryWrapper.orderByAsc(ExcellentLabeler::getRanking);
        if (limit != null && limit > 0) {
            queryWrapper.last("LIMIT " + limit);
        }
        return this.list(queryWrapper);
    }
    
    @Override
    public List<ExcellentLabeler> getRankingByUserId(Integer year, Integer month, Integer limit, Long userId) {
        LambdaQueryWrapper<ExcellentLabeler> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExcellentLabeler::getUserId, userId);
        if (year != null) {
            queryWrapper.eq(ExcellentLabeler::getStatYear, year);
        }
        if (month != null) {
            queryWrapper.eq(ExcellentLabeler::getStatMonth, month);
        }
        queryWrapper.orderByAsc(ExcellentLabeler::getRanking);
        if (limit != null && limit > 0) {
            queryWrapper.last("LIMIT " + limit);
        }
        return this.list(queryWrapper);
    }

    @Override
    public boolean refreshRanking(Integer year, Integer month) {
        // 这里需要实现具体的排名刷新逻辑
        // 由于涉及复杂业务逻辑，暂时返回true
        return true;
    }
    
    @Override
    public boolean refreshRankingByUserId(Integer year, Integer month, Long userId) {
        // 这里需要实现具体的排名刷新逻辑
        // 由于涉及复杂业务逻辑，暂时返回true
        return true;
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<ExcellentLabeler> queryWrapper) {
        queryWrapper.eq(ExcellentLabeler::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(ExcellentLabeler entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}